package com.lgj.auth.code.customize.weChat;

import com.lgj.auth.code.service.CustomerUserDetailService;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;

import java.util.HashSet;

/**
 * @author: guangjie.liao
 * @Date: 2023/08/27 21:06
 * @Description: 微信登陆授权
 */
public class WeChatAuthenticationProvider implements AuthenticationProvider {

    private UserDetailsService userDetailsService;

    public WeChatAuthenticationProvider(UserDetailsService userDetailsService){
        this.userDetailsService = userDetailsService;
    }

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        //微信授权码
        String verificationCode = (String) authentication.getCredentials();

        //TODO 微信授权码获取微信token

        //TODO 微信授权码获取微信用户信息

        //TODO 校验本地用户信息和微信用户信息
        //账号信息验证
        UserDetails userDetails = ((CustomerUserDetailService) userDetailsService).loadUserByUsername(null);
        WeChatAuthenticationToken result = new WeChatAuthenticationToken(userDetails, authentication.getCredentials(), new HashSet<>());
        result.setDetails(authentication.getDetails());
        return result;
    }

    @Override
    public boolean supports(Class<?> authentication) {
        return WeChatAuthenticationProvider.class.isAssignableFrom(authentication);
    }
}
